package com.lagou.service.impl;

import com.lagou.dao.RoleMapper;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public List<Role> findAllRole(Role role) {

        List<Role> allRole = roleMapper.findAllRole (role);

        return allRole;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {

        List<Integer> menuByRoleId = roleMapper.findMenuByRoleId (roleId);

        return menuByRoleId;
    }


    /*为角色分配菜单信息*/
    @Override
    public void RoleContextMenu(RoleMenuVo roleMenuVo) {

        //1.清空中间表的关联关系
        roleMapper.deleteRoleContextMenu (roleMenuVo.getRoleId ());

        //2.为角色分配菜单
        for(Integer mid:roleMenuVo.getMenuIdList ()) {
            Role_menu_relation role_menu_relation = new Role_menu_relation ();
            role_menu_relation.setRoleId (roleMenuVo.getRoleId ());
            role_menu_relation.setMenuId (mid);
            role_menu_relation.setCreatedTime (new Date ());
            role_menu_relation.setUpdatedTime (new Date ());
            role_menu_relation.setCreatedBy ("system");
            role_menu_relation.setUpdatedby ("system");
            roleMapper.RoleContextMenu (role_menu_relation);
        }
    }

    /*删除角色*/
    @Override
    public void deleteRole(Integer id) {
        // 清空中间表
        roleMapper.deleteRoleContextMenu (id);
        roleMapper.deleteRole (id);
    }

    /*查询该角色拥有的资源分类信息和资源信息*/
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {

        //查询该角色拥有的资源信息
        List<Resource> resourceList = roleMapper.findResourceByRoleId (roleId);
        if (resourceList.isEmpty ()) {
            return null;
        }

        //查询该角色拥有的资源分类信息
        List<ResourceCategory> resourceCategoryList = roleMapper.findResourceCategoryByRoleId (roleId);


        //将资源封装到对应的分类下
        for (Resource resource:resourceList) {
            for (ResourceCategory resourceCategory:resourceCategoryList) {
                if (resource.getCategoryId ().equals (resourceCategory.getId ())){
                    resourceCategory.getResourceList ().add (resource);
                    break;
                }
            }
        }

        return resourceCategoryList;
    }

    /*分配角色的方法*/
    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {

        //删除角色的关联关系
        roleMapper.deleteRoleResourceRelationById (roleResourceVo.getRoleId ());

        //补全信息,建立新的关联关系
        for (Integer rId:roleResourceVo.getResourceIdList ()) {
            //新建中间表数据
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation ();
            roleResourceRelation.setResourceId (rId);
            roleResourceRelation.setRoleId (roleResourceVo.getRoleId ());

            //封装数据
            Date date = new Date ();
            roleResourceRelation.setCreatedTime (date);
            roleResourceRelation.setUpdatedTime (date);
            roleResourceRelation.setCreatedBy ("system");
            roleResourceRelation.setUpdatedBy ("system");
            roleMapper.roleContextResource (roleResourceRelation);
        }

    }


}
